<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmp">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工用户" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="电子邮箱" width="120" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="120" sortable>
			</el-table-column>
			<el-table-column prop="salt" label="盐值" width="120" sortable>
			</el-table-column>
			<el-table-column prop="password" label="员工密码" width="120" sortable>
			</el-table-column>
			<el-table-column prop="age" label="员工年龄" width="100" >
			</el-table-column>
			<el-table-column prop="state" label="员工状态" width="120" sortable>
				<template scope="scope">
					<span size="small" style="color:green" v-if="scope.row.state==1">在职</span>
					<span size="small" style="color:red" v-else-if="scope.row.state==0">离职</span>
					<span size="small" style="color:pink" v-else>待离职</span>
				</template>
			</el-table-column>
			<el-table-column prop="department.name" label="部门" width="120" sortable>
			</el-table-column>
			<el-table-column prop="logininfo.username" label="登录昵称" width="150" sortable>
			</el-table-column>
			<el-table-column prop="shop.name" label="店铺名称" width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="300">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 批量删除和 -->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="true">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="员工姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="员工邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="盐值" prop="salt">
					<el-input v-model="editForm.salt" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="员工密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="员工年龄" prop="age">
					<el-input v-model="editForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="员工状态">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" :label="1">在职</el-radio>
						<el-radio class="radio" :label="0">离职</el-radio>
						<el-radio class="radio" :label="null">其他</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="对应部门" prop="department">
					<el-select v-model="editForm.department" value-key="id" placeholder="请选择">
						<el-option clearable
								   v-for="dept in departments"
								   :key="dept.id"
								   :label="dept.name"
								   :value="dept">
							<span style="float: left">{{ dept.name}}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ dept.id}}</span>
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="对应店铺" prop="shop">
					<el-select v-model="editForm.shop" value-key="id" placeholder="请选择">
						<el-option clearable
								   v-for="emp in shops"
								   :key="emp.id"
								   :label="emp.name"
								   :value="emp">
							<span style="float: left">{{ emp.name}}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ emp.id}}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>

</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
                editLoading: false,
			    shops: [],
                departments: [],
                editFormVisible: false,
                title: '',
                sels: [],
                total: 0,
                pageSize: 5,
                employees: [],
                currentPage: 1,
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
                listLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
                filters: {
				 	name: ""
				},
                editForm: {
					age: null,
					department:{},
					email: '',
					logininfo: {},
					password: '',
					phone: '',
					salt: '',
					state: null,
					username: '',
					shop: {}
				},
                editFormRules: { // 必填项
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
			}
		},
		methods: {
            selsChange(sels) {
                this.sels = sels;
            },
            getEmp(){
              let para = {
                  currentPage: this.currentPage,
				  pageSize: this.pageSize,
				  queryName: this.filters.name
			  };
			  this.listLoading = true; // 展示忙等框
              this.$http.post("/employee", para)
				  .then(result => {
				      this.listLoading = false;
				      console.log(result);
				      this.employees = result.data.data;
				      this.total = result.data.totals;
				  })
				  .catch(result => {
                      this.listLoading = false;
                      this.$message({
                          message: "系统忙111",
                          type: 'error'
                      });
				  })
			},
            handleAdd(){ // 展示添加模态框
				this.title = "新增";
                this.editFormVisible = true;
                this.editForm = {
                    age: null,
					department:{},
                    email: '',
					logininfo: {},
                    password: '',
					phone: '',
					salt: '',
					state: null,
					username: ''
                }

			},
            handleEdit(){ // 展示添加模态框

            },
            batchRemove(){ // 批量删除
                let ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => { // 确认删除
                    this.listLoading = true;
                    this.$http.patch("/employee", ids)
						.then(result => {
                            if (result.data.success){
                                this.$message({
                                    message: result.data.message,
                                    type: 'success'
                                });
                                this.handleCurrentChange(1);
                            }else {
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                                this.handleCurrentChange(1);
                            }
						})
						.catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统忙---批量删除",
                                type: 'error'
                            });
                            this.handleCurrentChange(1);
						})
				}).catch (() => { // 取消删除

				})
			},
            handleDel(index, row){ // 删除
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete("/employee/" + row.id)
                        .then(result => {
                            this.listLoading = false;
                            if (result.data.success){
                                this.$message({
                                    message: result.data.message,
                                    type: 'success'
                                });
                                this.handleCurrentChange(1);
                            }else {
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                                this.handleCurrentChange(1);
                            }
                        })
                        .catch(result => {
                            this.listLoading = false;
                            this.$message({
                                message: "系统忙---删除",
                                type: 'error'
                            });
                            this.handleCurrentChange(1);
                        })
				})
			},
            handleCurrentChange(currentPage){ // 页码变法时触发, 参数为切换后的页码
				this.currentPage = currentPage;
				this.getEmp();
			}
		},
		mounted() {
			this.getEmp();
		}
    }


</script>

<style scoped>

</style>